<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="../css/mui.min.css" />
		<link rel="stylesheet" href="../css/icons-extra.css" />
		<link rel="stylesheet" href="../css/detail.css" />
	</head>

	<body>
		<div class="mui-content">
			<!--顶部banner图 开始-->
			<div id="kr-article-banner" class="kr-article-banner">
				<div id="kr-article-cover" class="kr-article-cover">
				</div>
				<h2 id="kr-article-title" class="kr-article-title"></h2>
			</div>
			<!--顶部banner图 结束-->

			<div class="kr-article-content">
				<!-- 文章作者、发布时间等信息 -->
				<div class="kr-article-meta">
					<div id="kr-article-author" class="kr-article-author"></div>
					<div class="kr-article-text">发表于</div>
					<div id="kr-article-time" class="kr-article-time"></div>
				</div>
				<!--文章详细内容-->
				<div id="kr-article-article" class="kr-article-article"></div>
			</div>
		</div>
		<script type="text/javascript" src="../js/lib/mui.min.js"></script>
		<script type="text/javascript" src="../js/lib/h.min.js"></script>
		<script type="text/javascript">
			var shareMsg = {};
			mui.init({
				swipeBack: true
			});
			//监听自定义事件，获取新闻详情
			document.addEventListener('get_detail', function(event) {
				h('#kr-article-cover').html("<img src='" + event.detail.cover + "'/>");
				h('#kr-article-time').html(event.detail.time);
				h('#kr-article-title').html(event.detail.title.replace("\"", "&quot;"));
				shareMsg.title = event.detail.title;
				shareMsg.thumbs = [event.detail.cover];
				shareMsg.pictures = [event.detail.cover]
				shareMsg.content = event.detail.digest
				mui.get('http://c.m.163.com/nc/article/' + event.detail.docid + '/full.html', function(data) {
					var html = data[event.detail.docid].body;
					for(i=0;i<data[event.detail.docid].img.length;i++){
						html=html.replace(data[event.detail.docid].img[i].ref,'<img src="'+data[event.detail.docid].img[i].src+'">')
					}
					h('#kr-article-article').html(html);
					shareMsg.href = data[event.detail.docid].shareLink;
				}, 'json');
			});
			document.addEventListener('showShare', function(e) {
				showShare();
			});

			//重写返回逻辑
			mui.back = function() {
				plus.webview.currentWebview().hide("auto", 300);
			}

			//窗口隐藏时，重置页面数据
			mui.plusReady(function() {
				var self = plus.webview.currentWebview();
				self.addEventListener("hide", function(e) {
					window.scrollTo(0, 0);
					shareMsg = {};
					h('#kr-article-cover').html('');
					h('#kr-article-time').html('');
					h('#kr-article-title').html('');
					h('#kr-article-article').html('');
				}, false);
				updateSerivces();
			})

			var Intent = null,
				File = null,
				Uri = null,
				main = null;
			var shares = null;
			var shareImageUrl = '';

			/**
			 * 更新分享服务
			 */
			function updateSerivces() {
				plus.share.getServices(function(s) {
					shares = {};
					for(var i in s) {
						var t = s[i];
						shares[t.id] = t;
					}
					//					outSet("获取分享服务列表成功");
				}, function(e) {
					outSet("获取分享服务列表失败：" + e.message);
				});
			}

			/**
			 * 分享按钮点击事件
			 */
			function showShare() {
				var ids = [{
						id: "weixin",
						ex: "WXSceneSession" /*微信好友*/
					}, {
						id: "weixin",
						ex: "WXSceneTimeline" /*微信朋友圈*/
					}],
					bts = [{
						title: "发送给微信好友"
					}, {
						title: "分享到微信朋友圈"
					}];
				plus.nativeUI.actionSheet({
						cancel: "取消",
						buttons: bts
					},
					function(e) {
						var i = e.index;
						if(i > 0) {
							shareAction(ids[i - 1].id, ids[i - 1].ex);
						}
					}
				);
			}
			/**
			 * 分享操作
			 */
			function shareAction(id, ex) {
				var s = null;
				if(!id || !(s = shares[id])) {
					outLine("无效的分享服务！");
					return;
				}
				if(s.authenticated) {
					outSet("---已授权---");
					shareMessage(s, ex);
				} else {
					outSet("---未授权---");
					s.authorize(function() {
						shareMessage(s, ex);
					}, function(e) {
						outLine("认证授权失败");
					});
				}
			}

			/**发送分享消息
			 * @param {Object} s
			 * @param {Object} ex
			 */
			function shareMessage(s, ex) {
				shareMsg.extra = {
					scene: ex
				};
				s.send(shareMsg, function() {
					outLine("分享成功!");
				}, function(e) {
					console.log(JSON.stringify(e));
					outLine("分享失败!");
				});
			}

			// 控制台输出日志
			function outSet(msg) {
				console.log(msg);
			}
			// 界面弹出吐司提示
			function outLine(msg) {
				mui.toast(msg);
			}
		</script>
	</body>

</html>